#include <cstdio>
#include "glad/glad.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include "GUI.hpp"

GUI::GUI(Engine* engine_)
{
	engine = engine_;
	ImGui::CreateContext();
	ImGui_ImplGlfw_InitForOpenGL(engine->camera->window, true);
	ImGui_ImplOpenGL3_Init("#version 300 es");
}
void GUI::draw()
{
	ImGui_ImplOpenGL3_NewFrame();
	ImGui_ImplGlfw_NewFrame();
	ImGui::NewFrame();



	ImGui::Begin("Simple Game Engine", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
	{
		ImGui::SetWindowSize(ImVec2(1280.f, 0.f));
		ImGui::SetWindowPos(ImVec2(0.f, 0.f));


		ImGui::BeginMenuBar();
		{
			if (ImGui::BeginMenu("File"))
			{
				ImGui::MenuItem("Open", "Ctrl+O");
				ImGui::MenuItem("Save", "Ctrl+S");
				ImGui::MenuItem("Quit", "Ctrl+Q");
        ImGui::EndMenu();
			}
		}
		ImGui::EndMenuBar();
	}
	ImGui::End();



	ImGui::Begin("FPS");
	ImGui::Text("%d", static_cast<int>(floorf(1.0f / engine->period)));
	ImGui::End();


	ImGui::Begin("SceneTree", nullptr, ImGuiWindowFlags_AlwaysVerticalScrollbar);
	{
		/*for (uint8_t i = 0; i < engine->pointlights.size(); ++i)
		{
			if (ImGui::Button(("Light " + std::to_string(i)).c_str()))
			{
			}
		}
		for (uint8_t i = 0; i < engine->spotlights.size(); ++i)
		{
			if (ImGui::Button(("Light " + std::to_string(i)).c_str()))
			{
			}
		}

		for (uint8_t i = 0; i < engine->objects.size(); ++i)
		{
			if (ImGui::Button(("Object " + std::to_string(i)).c_str()))
			{
				sel_o = i;
				engine->Highlight(i);
			}
		}*/
	}
	ImGui::End();


	/*if (sel_o > -1)
	{
		ImGui::Begin(("Object " + std::to_string(sel_o)).c_str());
		{
			ImGui::DragFloat3("Position", &engine->objects[sel_o]->position.x, 0.5f, -10000.f, 10000.f);
			engine->teleportObject(sel_o, engine->objects[sel_o]->position);

			glm::vec3 r = engine->objects[sel_o]->rotation;
			ImGui::DragFloat3("Rotation", &r.x, 0.5f, -180.f, 180.f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
			engine->setObjectRotation(sel_o, r);

			glm::vec3 s = engine->objects[sel_o]->scale;
			ImGui::DragFloat3("Scale", &s.x, 0.05f, 0.005f, 90.f);
			engine->scaleObject(sel_o, s);
		}
		ImGui::End();
	}*/



	ImGui::Render();
	ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
GUI::~GUI()
{
	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();
}
